package org.easydarwin.easyplayer.zhi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import org.easydarwin.bean.Login;
import org.easydarwin.easyplayer.PlaylistActivity;
import org.easydarwin.easyplayer.R;
import org.easydarwin.http.API;
import org.easydarwin.http.JSONUtils;
import org.easydarwin.http.Okhttp;
import org.easydarwin.http.SpfUtils;


import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


public class LoginActivity extends Activity {

    @BindView(R.id.login_phone)
    EditText loginPhone;
    @BindView(R.id.login_mm)
    EditText loginMm;
    @BindView(R.id.tv_login)
    TextView butLogin;
    @BindView(R.id.but_zhu)
    TextView butRegist;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);

    }

    @OnClick({R.id.tv_login, R.id.but_zhu, R.id.but_IP, R.id.but_xiugai})
    public void onViewClicked(View view) {
        switch (view.getId()) {

            case R.id.tv_login:
                if (TextUtils.isEmpty(loginPhone.getText().toString())) {
                    Toast.makeText(this, "请输入账号", Toast.LENGTH_SHORT).show();
                    return;
                } else if (TextUtils.isEmpty(loginMm.getText().toString())) {
                    Toast.makeText(this, "请密码", Toast.LENGTH_SHORT).show();
                    return;
                }

                API.login(loginPhone.getText().toString(), loginMm.getText().toString(), new Okhttp.Objectcallback() {
                    @Override
                    public void onsuccess(String st) {
                        Log.e("99", "登录" + st);
                        Login register = JSONUtils.parseJSON(st, Login.class);
                        if (register.getCode() == 10001) {
                            if (register.isIssuccess()) {
                                Toast.makeText(LoginActivity.this, register.getMsg(), Toast.LENGTH_SHORT).show();
                                startActivity(new Intent(LoginActivity.this, PlaylistActivity.class));
                                SpfUtils.put(LoginActivity.this, "islogin", true);
                                SpfUtils.put(LoginActivity.this, "name", loginPhone.getText().toString());
                                SpfUtils.put(LoginActivity.this, "user_uid", register.getData().getUser_uid());
                                SpfUtils.put(LoginActivity.this, "user_name", register.getData().getUser_name());
                                SpfUtils.put(LoginActivity.this, "sex", register.getData().getSex());
                                SpfUtils.put(LoginActivity.this, "location", register.getData().getLocation());
                                finish();
                            } else {
                                Toast.makeText(LoginActivity.this, register.getMsg(), Toast.LENGTH_SHORT).show();
                            }
                        } else {

                        }

                    }

                    @Override
                    public void onFalia(int code, String errst) {
                        Toast.makeText(LoginActivity.this, "登录网络错误！" + errst.toString(), Toast.LENGTH_SHORT).show();
                        Log.e("99", "登录" + errst.toString());
                    }
                });


                break;


            case R.id.but_IP:
                startActivity(new Intent(this, SetIp_Activity.class));
                break;
            case R.id.but_zhu:
                startActivity(new Intent(this, RegistActivity.class));
                break;
            case R.id.but_xiugai:
                 startActivity(new Intent(this, XiuGaiActivity.class));
                break;


        }
    }
}
